﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using mal.balades.Business.DataModel;
using mal.balades.Business.Log;
using mal.balades.Business.Web;
using mal.Web.Routing;

namespace mal.balades.Web.Views
{
    public partial class VisitsList : PageBase
    {
        private const string CategoryIdKey = "catId";
        private const string CategoryUrlNameKey = "catUrl";
        private int CategoryId = int.MinValue;
        private string CategoryUrlName = string.Empty;
        

        protected void Page_Load(object sender, EventArgs e)
        {
            MalLogManager.WebLogger.Info("");

            if (Request[CategoryIdKey] != null)
            {
                int.TryParse(Request[CategoryIdKey], out CategoryId);
            }

            if (PageRouteHandler.QueryString[CategoryIdKey] != null)
            {
                int.TryParse(PageRouteHandler.QueryString[CategoryIdKey].ToString(), out CategoryId);
            }


            if (Request[CategoryUrlNameKey] != null)
            {
                CategoryUrlName = Request[CategoryUrlNameKey];
            }

            if (PageRouteHandler.QueryString[CategoryUrlNameKey] != null)
            {
                CategoryUrlName = PageRouteHandler.QueryString[CategoryUrlNameKey].ToString();
            }  

            if (!Page.IsPostBack)
            {
                //this.gvVisits.DataSource = GetVisits();
                //this.gvVisits.DataBind();

                this.rptVisits.DataSource = GetVisits();
                this.rptVisits.DataBind();
            }
        }

        private List<VisitList> GetVisits()
        {
            

            List<VisitList> visits = null;

            if (CategoryId != int.MinValue)
            {

                MalLogManager.WebLogger.InfoFormat("CategoryId:{0}", CategoryId);

                visits = BaladesManager.GetAllVisits().Where
                    (v => v.PlaceCategoryID == CategoryId || v.MarketingCategoryID == CategoryId).ToList();


                Category category = BaladesManager.GetCateogries().FirstOrDefault(c => c.CategoryID == CategoryId);
                if (category != null)
                {
                    this.Title = category.Name;

                    h1Title.InnerText = category.Name;
                    imgCat.ImageUrl = CategoryPictureUrl(category.Image);
                    lblTextCat.Text = category.Text;

                    headerDetail.Visible = true;
                }
            }
            else if (!string.IsNullOrEmpty(CategoryUrlName))
            {
                MalLogManager.WebLogger.InfoFormat("CategoryUrlName:{0}", CategoryUrlName);

                Category category = BaladesManager.GetCateogries().FirstOrDefault(c => c.UrlName.ToLower() == CategoryUrlName.ToLower());
                if (category != null)
                {
                    this.Title = category.Name;

                    h1Title.InnerText = category.Name;
                    imgCat.ImageUrl = CategoryPictureUrl(category.Image);
                    lblTextCat.Text = category.Text;
                    headerDetail.Visible = true;

                    visits = BaladesManager.GetAllVisits().Where
                  (v => v.PlaceCategoryID == category.CategoryID || v.MarketingCategoryID == category.CategoryID).ToList();
                }
            }
            else
            {
                MalLogManager.WebLogger.Info("All visits");
                this.Title = "Toutes les balades";
                visits = BaladesManager.GetAllVisits().ToList();
                headerDetail.Visible = false;

            }

            return visits;
        }

        //public string CategoryPictureUrl(object url)
        //{
        //    return string.Concat("/images/", url);
        //}

        //public string VisitPictureUrl(object url)
        //{
        //    return string.Concat("http://autourdeaix.free.fr/photos/", url,".gif");
        //}

        public string FormatDateTime(object value, string format)
        {
            DateTime timeSpan = (DateTime)value;
            return timeSpan.ToShortTimeString();
        }

        protected void gvVisits_PageIndexChanged(object sender, EventArgs e)
        {
        }

        //protected void gvVisits_PageIndexChanging(object sender, GridViewPageEventArgs e)
        //{
        //    if (gvVisits.EditIndex != -1)
        //    {
        //        // Use the Cancel property to cancel the paging operation.
        //        e.Cancel = true;
        //    }
        //    else
        //    {

        //        gvVisits.PageIndex = e.NewPageIndex;
        //        gvVisits.DataSource = GetVisits();
        //        gvVisits.DataBind();
        //    }

        //}
    }
}